package link.anyauto.j2m;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import link.anyauto.j2m.service.ForegroundService;
import link.anyauto.j2m.service.ShortTermService;
import link.anyauto.srv.service.SsoAuth;

public class MainAct extends AppCompatActivity {

    @Bind(R.id.btn_sso)
    Button btnSso;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        ButterKnife.bind(this);
        startService(new Intent(ForegroundService.FOREGROUND_ACTION));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra1"));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra2"));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra3"));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra4"));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra5"));
        startService(new Intent(ShortTermService.SHORT_TERM_ACTION).putExtra(ShortTermService.KEY_EXTRA, "extra6"));
    }

    @OnClick(R.id.btn_sso)
    public void sso(View view) {
        if (auth == null) {
            bind();
        } else  {
            doAuth();
        }
    }

    @OnClick(R.id.btn_content_provider)
    public void provider() {
        startActivity(new Intent(this, ProviderAct.class));
    }

    @OnClick(R.id.btn_simple_image_view)
    public void simpleImageView() {
        startActivity(new Intent(this, SimpleImageViewAct.class));
    }

    @OnClick(R.id.btn_multi_thread)
    public void multiThread() {
        startActivity(new Intent(this, MultiThreadAct.class));
    }

    @OnClick(R.id.btn_executor)
    public void executor() {
        startActivity(new Intent(this, ExecutorAct.class));
    }

    void bind(){
        bindService(new Intent("link.anyauto.sso.SSO_SERVICE"), conn, Context.BIND_AUTO_CREATE);
    }

    void unbind() {
        if(auth != null)
            unbindService(conn);
    }

    void doAuth() {
        try {
            auth.ssoAuth("hello world", "password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbind();
    }

    SsoAuth auth;

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            auth = SsoAuth.Stub.asInterface(iBinder);

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            auth = null;
        }
    };
}
